package com.jule.teenpatti.game.network.protocol.reqs;

import JoloProtobuf.GameSvr.JoloGame;
import com.jule.core.common.log.LoggerUtils;
import com.jule.teenpatti.base.bean.ItemBean;
import com.jule.teenpatti.base.bean.UnitVO;
import com.jule.teenpatti.base.enums.GameConst;
import com.jule.teenpatti.base.service.ItemServer;
import com.jule.teenpatti.game.config.Config;
import com.jule.teenpatti.game.network.protocol.ClientReq;
import com.jule.teenpatti.game.network.protocol.acks.JoloGame_TestUnlockAck_50055;
import com.jule.teenpatti.game.play.AbstractTable;
import com.jule.teenpatti.game.service.TableService;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JoloGame_TestUnlockReq_50055 extends ClientReq {
    private long time;
    private JoloGame.JoloGame_UnlockReq req;

    public JoloGame_TestUnlockReq_50055(int functionId) {
        super(functionId);
    }

    @Override
    public void readPayLoadImpl(ByteBuf buf) throws Exception {
        if(!Config.CAN_UNLOCK_ITEM){
            log.error("error: Have no right to unlock");
            return;
        }
        time = System.currentTimeMillis();
        byte[] blob = new byte[buf.readableBytes()];
        buf.readBytes(blob);
        req = JoloGame.JoloGame_UnlockReq.parseFrom(blob);

        AbstractTable table = TableService.getInstance().getTable(header.gameId + "", req.getRoomId(), req.getTableId());
        if (null != table) {
            this.setTable(table);
        } else {
            log.error("桌子不存在,roomId->" + req.getRoomId() + ", tableId->" + req.getTableId());
        }
    }

    @Override
    public void processImpl() throws Exception {
        try {
            log.debug("收到消息-> " + functionId + " reqNum-> " + header.reqNum + " " + req.toString());
            int itemId = Integer.parseInt(req.getItemId());
            String userId = req.getUserId();
            AbstractTable table = getTable();
            if (table == null) {
                log.info("taskListReq, table is null");
                return;
            }

            ItemServer.OBJ.addUnit(Config.GAME_ID, userId, itemId, 1, "TestUnlock");

            UnitVO unitVO = ItemServer.OBJ.getUnit(Config.GAME_ID, userId, itemId);
            if (unitVO.getResult() == 0) {
                ItemBean itemBean = unitVO.getItems().get(0);
                if (itemBean != null) {
                    JoloGame.JoloGame_ItemStatusInfo.Builder itemStatusInfo = JoloGame.JoloGame_ItemStatusInfo.newBuilder();
                    itemStatusInfo.setNum(itemBean.getItemNum()).setItemId(req.getItemId()).setCountDown(itemBean.getTimeOut());

                    JoloGame.JoloGame_UnlockAck.Builder ack = JoloGame.JoloGame_UnlockAck.newBuilder();
                    ack.setRoomId(table.getRoomId()).setTableId(table.getTableId()).setUserId(req.getUserId())
                            .setItemStatusInfoList(itemStatusInfo).setResult(1);
                    ctx.writeAndFlush(new JoloGame_TestUnlockAck_50055(ack.build(), header));
                }
            }
            long timeMillis = System.currentTimeMillis() - time;
            if(timeMillis> GameConst.COST_TIME) {
                LoggerUtils.performance.info("TestUnlockReq_50055,cost time:{},req:{}",timeMillis , req.toString());
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }finally {
        }
    }
}
